package com.yxhl.zoume.core.specialcar.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.yxhl.protobuf.BizOrder;
import com.yxhl.protobuf.BizOrderRequest;
import com.yxhl.protobuf.BizOrderResponse;
import com.yxhl.protobuf.CommonReqParams;
import com.yxhl.protobuf.CommonRespParams;
import com.yxhl.protobuf.OrderBizType;
import com.yxhl.protobuf.OrderSrcType;
import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.specialcar.model.BizType;
import com.yxhl.zoume.core.specialcar.model.SpecialCarPriceInfo;
import com.yxhl.zoume.core.specialcar.model.SpecialCarType;
import com.yxhl.zoume.core.specialcar.view.SpecialCarHomeView;
import com.yxhl.zoume.data.http.model.specialcar.SpecialCarPriceQueryResult;
import com.yxhl.zoume.data.http.rest.param.specialcar.SpecialCarOrderParam;
import com.yxhl.zoume.data.http.rest.param.specialcar.SpecialCarQueryParam;
import com.yxhl.zoume.data.http.rest.response.specialcar.SpecialCarPriceQueryResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.car.specialcar.GetSpecialCarOrderUseCase;
import com.yxhl.zoume.domain.interactor.car.specialcar.QueryLastUnFinishOrderUseCase;
import com.yxhl.zoume.domain.interactor.car.specialcar.QuerySpecialCarPriceUseCase;
import com.yxhl.zoume.utils.LOG;

import java.text.DecimalFormat;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by sugarya on 16/4/9.
 */
public class SpecialCarHomePresenter extends BasePresenter {

    private static final String TAG = "SpecialHomePresenter";

    @Inject
    QuerySpecialCarPriceUseCase mQuerySpecialCarPriceUseCase;
    @Inject
    QueryLastUnFinishOrderUseCase mQueryLastUnFinishOrderUseCase;
    @Inject
    GetSpecialCarOrderUseCase mGetSpecialCarOrderUseCase;

    private SpecialCarHomeView mSpecialCarHomeView;

    @Inject
    public SpecialCarHomePresenter(Context context) {
        super(context);
    }

    /**
     * 查询行程价格
     *
     * @param departStation
     * @param arrivalStation
     */
    public void queryTripPrice(String departStation, String arrivalStation) {
        SpecialCarQueryParam specialCarQueryParam = new SpecialCarQueryParam(
                getMobile(),
                getToken(),
                departStation,
                arrivalStation
        );
        mQuerySpecialCarPriceUseCase
                .execute(specialCarQueryParam,new ZMSubscriber<SpecialCarPriceQueryResponse>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mSpecialCarHomeView.renderSpecialCarOrderOnError();
                    }

                    @Override
                    public void onNext(SpecialCarPriceQueryResponse specialCarPriceQueryResponse) {
                        super.onNext(specialCarPriceQueryResponse);
                        if (specialCarPriceQueryResponse != null) {
                            LOG.e(TAG, specialCarPriceQueryResponse.toString());
                            boolean isSucc = specialCarPriceQueryResponse.isSucc();
                            if (isSucc) {
                                SpecialCarPriceInfo specialCarPriceInfo = new SpecialCarPriceInfo();

                                List<SpecialCarPriceQueryResult> schedules = specialCarPriceQueryResponse.getSchedules();
                                if (schedules != null && schedules.size() > 0) {
                                    if (schedules.size() >= 2) {
                                        for (SpecialCarPriceQueryResult specialCarPriceQueryResult : schedules) {
                                            String describeStr = specialCarPriceQueryResult.getLineDesc();
                                            specialCarPriceInfo.setLineDesc(describeStr);
                                            specialCarPriceInfo.setMaxSeat(specialCarPriceQueryResult.getMaxSeat());
                                            //线路价格
                                            String type = specialCarPriceQueryResult.getType();
                                            DecimalFormat df = new DecimalFormat("######0.00");

                                            if (!TextUtils.isEmpty(type)) {
                                                switch (type) {
                                                    case SpecialCarType.CHARTER:
                                                        String charterPrice = df.format(specialCarPriceQueryResult.getActualPrice());
                                                        specialCarPriceInfo.setCharterPrice(Double.parseDouble(charterPrice));

                                                        break;
                                                    case SpecialCarType.SHARE:
                                                        String sharePrice = df.format(specialCarPriceQueryResult.getActualPrice());
                                                        specialCarPriceInfo.setSharePrice(Double.parseDouble(sharePrice));
                                                        break;
                                                }
                                            }
                                        }
                                    }
                                    mSpecialCarHomeView.renderPriceQueryOnSuccess(specialCarPriceInfo);
                                }
                            } else {
                                String resultMessage = specialCarPriceQueryResponse.getResultMessage();
                                if (!TextUtils.isEmpty(resultMessage)) {
                                    mSpecialCarHomeView.renderSpecialCarOrderOnFailure(resultMessage);
                                }
                            }
                        }
                    }
                });
    }

    /**
     * 快车下单
     *
     * @param specialCarOrderParam
     */
    public void getSpecialCarOrder(final SpecialCarOrderParam specialCarOrderParam) {

        final BizOrderRequest.Builder bizOrderBuilder = BizOrderRequest.newBuilder();
        int bizType = specialCarOrderParam.getBizType();
        switch (bizType) {
            case BizType.ORDER_BIZ_CHARTER:
                bizOrderBuilder.setBizType(OrderBizType.ORDER_BIZ_CHARTER);
                break;
            case BizType.ORDER_BIZ_SHARE:
                bizOrderBuilder.setBizType(OrderBizType.ORDER_BIZ_SHARE);
                break;
        }
        bizOrderBuilder
                .setCommonReqParams(getCommonReqParams())
                .setDepartTime(specialCarOrderParam.getDepartTime())
                .setStart(specialCarOrderParam.getStart())
                .setStartCityCode(specialCarOrderParam.getStartCode())
                .setEnd(specialCarOrderParam.getEnd())
                .setEndCityCode(specialCarOrderParam.getEndCityCode())
                .setLng(specialCarOrderParam.getLng())
                .setLat(specialCarOrderParam.getLat())
                .setElng(specialCarOrderParam.getElng())
                .setElat(specialCarOrderParam.getElat())
                .setAmount(specialCarOrderParam.getAmount())
                .setMobile(getMobile())
                .setSrc(OrderSrcType.APP);

        BizOrderRequest bizOrderRequest = bizOrderBuilder.build();

        mGetSpecialCarOrderUseCase.execute(bizOrderRequest,new ZMSubscriber<BizOrderResponse>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mSpecialCarHomeView.renderSpecialCarOrderOnCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mSpecialCarHomeView.renderSpecialCarOrderOnError();
                    }

                    @Override
                    public void onNext(BizOrderResponse bizOrderResponse) {
                        super.onNext(bizOrderResponse);
                        if (bizOrderResponse != null) {
                            CommonRespParams commonRespParams = bizOrderResponse.getCommonRespParams();
                            String isSucc = commonRespParams.getIsSucc();
                            if ("true".equalsIgnoreCase(isSucc)) {
                                List<BizOrder> bizOrderList = bizOrderResponse.getOrderFieldsesList();
                                if (bizOrderList != null && bizOrderList.size() > 0) {
                                    String specialCarOrderId = bizOrderList.get(0).getOrderSerial();
                                    mSpecialCarHomeView.renderSpecialCarOrderOnSuccess(specialCarOrderId);
                                }
                            } else {
                                //未完成的订单
                                String resultMessage = commonRespParams.getResultMsg();
                                mSpecialCarHomeView.renderSpecialCarOrderOnFailure(resultMessage);
                            }
                        }
                    }
                });


    }

    public void queryLastUnFinishOrder() {

        CommonReqParams commonReqParams = getCommonReqParams();
        final BizOrderRequest bizOrderRequest = BizOrderRequest.newBuilder()
                .setCommonReqParams(commonReqParams)
                .setMobile(getMobile())
                .setSrc(OrderSrcType.APP)
                .build();

        mQueryLastUnFinishOrderUseCase.execute(bizOrderRequest,new ZMSubscriber<BizOrderResponse>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mSpecialCarHomeView.renderUnFinishOrderOnCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mSpecialCarHomeView.renderUnFinishOrderOnCompleted();
                    }

                    @Override
                    public void onNext(BizOrderResponse bizOrderResponse) {
                        super.onNext(bizOrderResponse);
                        if (bizOrderResponse != null) {
                            CommonRespParams commonRespParams = bizOrderResponse.getCommonRespParams();
                            String isSucc = commonRespParams.getIsSucc();
                            if ("true".equalsIgnoreCase(isSucc)) {
                                List<BizOrder> bizOrderList = bizOrderResponse.getOrderFieldsesList();
                                String orderId;
                                if (bizOrderList != null && bizOrderList.size() > 0) {
                                    BizOrder bizOrder = bizOrderList.get(0);
                                    orderId = bizOrder.getOrderSerial();
                                } else {
                                    orderId = "";
                                }
                                mSpecialCarHomeView.renderUnFinishOrderOnSuccess(orderId);
                            }
                        }
                    }
                });
    }

    @Override
    public void attachView(BaseView view) {
        if (view instanceof SpecialCarHomeView) {
            mSpecialCarHomeView = (SpecialCarHomeView) view;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mGetSpecialCarOrderUseCase.unsubscribe();
        mQueryLastUnFinishOrderUseCase.unsubscribe();
        mQuerySpecialCarPriceUseCase.unsubscribe();
    }
}
